import axios from "axios"
import router from "../router";

let baseUrl = "";
if (process.env.NODE_ENV === 'development') {
    baseUrl = "http://localhost:5198";
    // baseUrl = "http://8.138.57.25:81";
    // baseUrl = "http://172.16.8.97:81/";
}
else {
    // baseUrl = "http://120.55.167.29:9091/";
    // baseUrl = "http://172.16.8.97:81/";
    // baseUrl = "http://sqxsh.zldcgroup.com:81";
    // baseUrl = "http://8.138.57.25:81";
    baseUrl = "https://wxl88.top";
}

export function requestPost(config) {
    //创建实例
    const instance = axios.create({
        baseURL: baseUrl,
        timeout: 1200000,
        method: "post",
        contentType: "application/json"
    })
    //拦截器
    instance.interceptors.request.use(config => {
        let token = localStorage.getItem("token") || "";
        config.headers.token = token;
        return config;
    }, err => {
        console.log(err)
    });
    

    instance.interceptors.response.use(res => {

        let data = res.data;

        if (data.success === false) {
            if (data.code === "000008") {
                //token 无效
                window.localStorage.clear();

                //跳转到登录页面
                // router.push('/login');
                router.push("/");
            }
        }
        return res.data;
    }, err => {
        console.log(err)
    });

    return instance(config);
}
export function requestGet(config) {
    //创建实例
    const instance = axios.create({
        baseURL: baseUrl,
        timeout: 1200000,
        method: "get",
        contentType: "application/json"
    })
    //拦截器
    instance.interceptors.request.use(config => {
        let token = localStorage.getItem("token") || "";
        config.headers.token = token;
        return config;
    }, err => {
        console.log(err)
    });

    instance.interceptors.response.use(res => {

        let data = res.data;

        if (data.success === false) {
            if (data.code === "000008") {
                //token 无效
                window.localStorage.clear();

                //跳转到登录页面
                // router.push('/login');
                router.push("/");
            }
        }
        return res.data;
    }, err => {
        console.log(err)
    });

    return instance(config);
}
